package cn.bfay.lion.rest.config;

import cn.bfay.lion.rest.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * InterceptorConfig.
 *
 * @author wangjiannan
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    private static String[] excludeUrl = new String[]{"/swagger-resources/**",
            "/v2/api-docs", "/swagger*", "/webjars/springfox-swagger-ui/**", "/error", "/static/**"};
    @Autowired
    private LoginInterceptor loginInterceptor;

    //private static String[] excludeUrl = new String[]{"/swagger-resources/**", "/v2/api-docs", "/swagger*",
    //        "/webjars/springfox-swagger-ui/**", "/error", "/static/**",
    //        "/page/**", "/wechat/**", "/user/**"};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置拦截和不拦截：配置不拦截的地址直接返回true,配置拦截的地址走拦截器方法,其他的地址直接返回true
        //registry.addInterceptor(loginInterceptor).excludePathPatterns("/page/login").addPathPatterns("/page/register");
        // 配置拦截和不拦截：配置不拦截的地址直接返回true,配置拦截的地址走拦截器方法,其他的地址直接返回true
        //registry.addInterceptor(loginInterceptor).addPathPatterns("/page/register").excludePathPatterns("/page/login");
        // 只配置不拦截的地址：配置不拦截的地址直接返回true，其他的走拦截器方法
        //registry.addInterceptor(loginInterceptor).excludePathPatterns("/page/login");
        // 只配置拦截的地址：配置拦截的地址走拦截器方法，其他的直接返回true
        //registry.addInterceptor(loginInterceptor).addPathPatterns("/page/register");
        // 不配置：全部走拦截器方法
        //registry.addInterceptor(loginInterceptor);
        // ---------------------------------
        registry.addInterceptor(loginInterceptor).excludePathPatterns(excludeUrl);
    }

    // 静态资源的配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        //registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        // 不需要设置templates,本来就能访问
        //registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }
}
